#include "tour.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h> 
#include <time.h>



void initp(pPoint a)
{
        a->x=0;
        a->y=0;
        a->z=0;
}


Point getvect(pPoint a,pPoint b)
{
        Point arg;
        arg.x=a->x-b->x;
		arg.z=a->z-b->z;
	//	if(globalY)
#ifdef globalY
        arg.y=a->y-b->y;
#endif
       
        return arg;
}


void movePoint(pPoint tomove,pPoint by)
{
        tomove->x+=by->x;
		tomove->z+=by->z;
#ifdef globalY
			tomove->y+=by->y;
#endif
        
}



void DrawWorld()
{
        drawOurCube();
        drawCubes();
}

void drawCubes()
{
  int i;
  for (i=0; i< CUBES; i++)
  {
    glPushMatrix();
                glRotated(cubeRot[i],0,1,0);
                glTranslated(-x[i] * 4, 0, -z[i] * 4);
                drawSingleCube();
    glPopMatrix();
  }
}


void drawSingleCube()
{
        glTranslated(0,2,0);
        glutSolidCube(4);
}

void drawOurCube()
{
        Point a=getvect(&Center,&Eye);
		float arg[4]={1,0,0,1};
		glMaterialfv( GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE, arg);

        glPushMatrix();
                glTranslated(Center.x,Center.y,Center.z);
				arg[0]= -1*180/PI* acos( a.x/sqrt( a.x*a.x+a.z*a.z ) );
				if( a.z <0 && a.x >0 || a.x <0 && a.z <0)
					arg[0]*=-1;
				
				glRotated(arg[0],0,1,0); // x / sqrt( x^2 + z^2)
#ifdef CubeOUR
				 glutSolidCube(4);
#else
				 glutSolidSphere(1.5,30,30);
#endif
					 
        glPopMatrix();
		arg[0]=0.3;
        arg[1]=0.3;
		arg[2]=0.3;
		glMaterialfv( GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE, arg);
    
}


void init()
{
  int i;
        Center.y=2;
        Center.x=0;
        Center.z=0;
        Eye.y=2;
        Eye.x=8;
        Eye.z=8;
        Up.y=8;
        Up.x=8;
        Up.z=8;

  for (i = 0; i< CUBES; i++)
  {
        z[i] = rand()%10;
        x[i] = rand()%10;
        cubeRot[i]=rand()%360;
  }


}


Point getnvect(pPoint a,pPoint b,float mulArgument)
{
        Point norm;
        norm= Normalize(getvect(a,b));
        norm.x*=mulArgument;
        norm.y*=mulArgument;
    norm.z*=mulArgument;
        return norm;
}

Point vectmul(Point a,Point b,float arg)
{
        Point rez;
        
        rez.x=a.y*b.z-a.z*b.y;
        rez.y=a.z*b.x-a.x*b.z;
        rez.z=a.x*b.y-a.y*b.z;

        rez=Normalize(rez);

        rez.x*=arg;
        rez.y*=arg;
        rez.z*=arg;

        return rez;
}

Point Normalize(Point a)
{
        float size=sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
        a.x=a.x/size;
        a.y=a.y/size;
        a.z=a.z/size;
        return a;
}
